// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'

// import url from '@/serviceAPI.dev.config.js'
import url from '@/serviceAPI.release.config.js'
import './assets/third-party/iconfont.css'

import axios from 'axios'
Vue.prototype.$axios = axios

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.prototype.$url = url;
Vue.config.productionTip = false;

import MetaInfo from 'vue-meta-info'
Vue.use(MetaInfo);

router.beforeEach((to, from, next) => {
  //console.log('start')
  NProgress.start();
  next()
});

router.afterEach(transition => {
  NProgress.done();
});

var getUrlParam = (name)=>{
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]); return null;
}
Vue.prototype.$getUrlParam = getUrlParam

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>',
  created(){
    //axios请求响应拦截器
    this.$axios.interceptors.request.use(
      config=>{
        // console.log(this.$store.state.token)
        console.log(localStorage.getItem('token'))
        if(localStorage.getItem('token')){
          config.headers.Authorization = localStorage.getItem('token')
        }
        return config
      },
      err=>{
        return Promise.reject(err);
      }
    ),
    this.$axios.interceptors.response.use(
      response=>{
        var token = response.headers.authorization
        if(token){
          this.$store.dispatch('refreshToken', token)
        }
        return response
      },
      error=>{
        console.log(error)
        return Promise.reject(error)
      }
    )
  }
})
